document.writeln("<div id='divNumberInput' style='position: absolute; z-index: 99999;visibility:hidden;'>" +
        "<input type='text' onpaste='return false' id='txtDecimalInput' class='input ' ShowAsPercentage='false' MaxValue='' MinValue='' AllowNull='true' style='width: 100px;ime-mode:disabled;" +
            "' RegInput='Nonnegative 10 Bit Decimal' />" +
        "</div>");

document.writeln("<div id='divDateInput' style='position: absolute; z-index: 99999; visibility:hidden'>" +
        "<input type='text' id='txtDateInput' onclick='SetDate(this);' RelativeTD='123' RegInput='' readonly class='input calendar' style='width: 100px;" +
            "'/>" +
        "</div>");


document.writeln("<div id='divTextInput' style='position: absolute; z-index: 99999;visibility:hidden;'>" +
        "<input type='text' id='txtTextInput' maxlength='200' class='input ' ShowAsPercentage='false' MaxValue='' MinValue='' style='width: 100px;ime-mode:disabled;" +
            "overflow: auto;' RegInput='' />" +
        "</div>");

document.writeln("<div id='divTextAreaInput' style='position: absolute; z-index: 99999;visibility:hidden;'>" +
        "<textarea class='textarea' name='textAreaInput' id='textAreaInput'></textarea>" +
        "</div>");

$(":text[RegInput]").bind("keypress", function(event) { return !this.readOnly && regInput(this, $(this).attr("RegInput"), null, event.keyCode || event.charCode); });
$(":text[RegInput]").bind("paste", function(event) { return regInput(this, $(this).attr("RegInput"), window.clipboardData.getData('Text')); });
$(":text[RegInput]").bind("drop", function(event) { return regInput(this, $(this).attr("RegInput"), null, event.dataTransfer.getData('Text')); });
$(":text[RegInput]").bind("change", function() { $(this.parentNode.parentNode.parentNode).next().children(":checkbox").attr("disabled", this.value == ""); });

//$("#textAreaInput").bind("keydown", function(event) { return !this.readOnly && regInputString2(this, $(this).attr("RegInput"), null, event.keyCode || event.charCode); });
////$("#textAreaInput").bind("paste", function(event) { return !this.readOnly && regInputString(this, $(this).attr("RegInput"), window.clipboardData.getData('Text')); });

function getScrollTop() {
    var scrollTop = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop;
    }
    else if (document.body) {
        scrollTop = document.body.scrollTop;
    }
    return scrollTop;
}

function getScrollLeft() {
    var scrollLeft = 0;
    if (document.documentElement && document.documentElement.scrollLeft) {
        scrollLeft = document.documentElement.scrollLeft;
    }
    else if (document.body) {
        scrollLeft = document.body.scrollLeft;
    }
    return scrollLeft;
}
//编辑单行文本类型
function EditTextInput(tdCell, RegInput, ShowAsPercentage) {

    EditInput(tdCell, 'txtTextInput', 'divTextInput', RegInput, ShowAsPercentage);
}

//编辑数值类型
function EditNumberInput(tdCell, RegInput, ShowAsPercentage, MaxValue, MinValue) {

    EditInput(tdCell, 'txtDecimalInput', 'divNumberInput', RegInput, ShowAsPercentage, MaxValue, MinValue);
}

//编辑日期类型
function EditDateInput(tdCell, RegInput) {

    EditInput(tdCell, 'txtDateInput', 'divDateInput', RegInput)
}

//编辑多行文本类型
function EditTextAreaInput(tdCell, RegInput) {
   
    EditInput(tdCell, 'textAreaInput', 'divTextAreaInput', RegInput)
}

function EditInput(tdCell, txt, div, RegInput, ShowAsPercentage, MaxValue, MinValue) {

    //alert(RegInput);
    
    var oldText = tdCell.innerText.Trim();
    tdCell.innerText = "";

    //var oldText = tdCell.innerText.Trim();
    var txtInput = document.getElementById(txt);

    $(txtInput).attr("RelativeTD", tdCell.id);

    if (RegInput !=  undefined) {
       // alert(RegInput);
        $(txtInput).attr("RegInput", RegInput);
    }

    if (ShowAsPercentage != undefined) {
        $(txtInput).attr("ShowAsPercentage", ShowAsPercentage);
    }

    if ($(txtInput).attr("ShowAsPercentage") == "true") {
        oldText = oldText.replace('%', '');
        //oldText = oldText/100;
    }

    txtInput.value = '';
    txtInput.value = oldText;



    txtInput.onblur = function() {
        EndEditInput(tdCell, txt, div, RegInput, ShowAsPercentage, MaxValue, MinValue);
    };
    

    var divBody = document.getElementById("Body");
    var top = -divBody.offsetTop, left = -divBody.offsetLeft, height = -10, width = -4;
    if (isIE()) {
        height = 0;
    }

    var txtWidth = (tdCell.getBoundingClientRect().right - tdCell.getBoundingClientRect().left - 24);
    txtWidth = txtWidth <= 100 ? 100 : txtWidth;

    tdCell.style.width = txtWidth + "px";
    
    var divTextEdit = document.getElementById(div);
    divTextEdit.style.left = (tdCell.getBoundingClientRect().left + getScrollLeft() + left) + "px";
    divTextEdit.style.top = (tdCell.getBoundingClientRect().top + getScrollTop() + top) + "px";
    
    divTextEdit.style.height = (tdCell.getBoundingClientRect().bottom - tdCell.getBoundingClientRect().top + height) + "px";
    divTextEdit.style.width = (txtWidth+20) + "px";


    txtInput.style.width = txtWidth + "px";
    tdCell.style.height = $(txtInput).parent().parent().height() + "px";
    
    divTextEdit.style.visibility = "visible";
    txtInput.focus();
}

function EndEditInput(tdCell, txt, div, RegInput, ShowAsPercentage, MaxValue, MinValue) {
    var txtInput = document.getElementById(txt);
    var divTextEdit = document.getElementById(div);
 
    if (RegInput != undefined && RegInput != 'String 2' && RegInput.length > 0 && txtInput.value.length > 0) {

        if (txtInput.value != 'NA') {
            txtInput.value = parseFloat(txtInput.value).toString();

            MaxValue = MaxValue != undefined ? MaxValue : 2147483647;
            MinValue = MinValue != undefined ? MinValue : 0;
            $(txtInput).val(CheckLimit($(txtInput).val(), MinValue, MaxValue, $(txtInput).attr("AllowNull")));
        }
    }
    
     var NewValue = txtInput.value;
    if (RegInput != undefined && RegInput == 'String 2') {
        if (NewValue.length > 2000) {
            alert('字符不要超过2000, 超过部分将被截断');
            //return false;

            NewValue = NewValue.substring(0, 2000);

        }
    }   
   
    
    
    if ($(txtInput).attr("ShowAsPercentage") == "true" && txtInput.value.length>0) {
        NewValue = txtInput.value + '%';
    }

    divTextEdit.style.visibility = "hidden";
    tdCell.innerText = NewValue;

    tdCell.style.height = "";
    tdCell.style.width = "";

    tdCell.onclick = function() {
        EditInput(tdCell, txt, div, RegInput, ShowAsPercentage, MaxValue, MinValue);
    };

    AfterEditCell(tdCell);//alert('end');
}

//$(":text[RegInput]").bind("blur", function() { });

//结束编辑日期
function EndEditDate(textbox) {
    var txt = $(textbox);
    txt.val(DateToStringUS(txt.attr("NewDate")));

    $("#" + txt.attr("RelativeTD")).html(txt.val());
    AfterEditCell($("#" + txt.attr("RelativeTD"))[0]);
}

function AfterEditCell(tdCell) {
    if (tdCell.onchange) {
        tdCell.onchange();
    }
 
 
 
 }
      